package com.matt.repository.impl;

import com.matt.domain.NoticeMessage;
import com.matt.foundation.query.NoticeQuery;
import com.matt.foundation.vo.NoticeMessageVo;
import com.matt.repository.NoticeMessageRepository;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.NoticeMessageNativeRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/12/6
 * *******************************
 */
@Component
public class NoticeMessageNativeRepositoryImpl extends BaseNativeSqlRepository implements NoticeMessageNativeRepository{


    @Override
    public List<NoticeMessage> findByConditions(NoticeQuery query) {
        String querySql = String.format("SELECT  n.category as category, n.message as message, n.linkId as linkId, n.ext as ext " +
                " from notice_message n where true ");

        if (StringUtils.isNotBlank(query.getStaffId())) {
            querySql += String.format(" and n.receiveId='%s' ", query.getStaffId());
        }

        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and n.areaId='%s' ", query.getAreaId());
        }

        if (Objects.nonNull(query.getDueStatus())) {
            querySql += String.format(" and n.dueStatus='%s' ", query.getDueStatus());
        }
        querySql += " order by n.created desc";
        querySql += String.format(" limit %s, %s ", query.getPageNum() * query.getPageSize() ,  query.getPageSize());
        List<NoticeMessage> objectList = sqlObjectList(querySql, NoticeMessage.class);
        return objectList;
    }


    @Override
    public Long countByConditions(NoticeQuery query) {
        String querySql = String.format("SELECT  count(n.id)" +
                " from notice_message n where true ");

        if (StringUtils.isNotBlank(query.getStaffId())){
            querySql += String.format(" and n.receiveId='%s' ", query.getStaffId());
        }

        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and n.areaId='%s' ", query.getAreaId());
        }

        if (Objects.nonNull(query.getDueStatus())){
            querySql += String.format(" and n.dueStatus='%s' ", query.getDueStatus());
        }

        List objectArrayList = sqlSingleList(querySql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }
}
